package com.service.sys;

import com.common.ErrorCodeEnum;
import com.model.base.BaseEntity;
import com.common.emums.DataStatus;
import com.common.exception.BusinessException;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;

import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageHelper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;

import com.model.pojo.sys.SysOperLog;
import com.dao.primary.sys.SysOperLogMapper;


/**
 * @Author: zql
 * @Description: 操作日志服务
 * @Data: 2021-08-06
 */
@Slf4j
@Service
public class SysOperLogService {

    @Resource
    SysOperLogMapper sysOperLogMapper;

    /**
     * @Author: zql
     * @Description: 创建操作日志
     * @param sysOperLog 被创建对象主体
     * @Data: 2021-08-06
     */
    public void create(SysOperLog sysOperLog) {
        String name = SecurityUtils.getUsername();
        sysOperLog.setCreateTime(null);
        sysOperLog.setUpdateTime(null);
        sysOperLog.setCreator(name);
        sysOperLog.setModifier(name);
        sysOperLogMapper.insertSelective(sysOperLog);
    }

    /**
     * @Author: zql
     * @Description: 创建操作日志列表
     * @param sysOperLogList 增加的对象主体列表
     * @Data: 2021-08-06
     */
    public void createList(List<SysOperLog> sysOperLogList) {
        String name = SecurityUtils.getUsername();
        sysOperLogList.forEach(o -> {
            o.setCreator(name);
            o.setModifier(name);
            o.setUpdateTime(null);
            o.setCreateTime(null);
        });
        sysOperLogMapper.insertListSelective(sysOperLogList);
    }

    /**
     * @Author: zql
     * @Description: 更新操作日志信息
     * @param sysOperLog 更新的对象主体内容
     * @Data: 2021-08-06
     */
    public void update(SysOperLog sysOperLog) {
        String name = SecurityUtils.getUsername();
        sysOperLog.setCreateTime(null);
        sysOperLog.setUpdateTime(null);
        sysOperLog.setCreator(null);
        sysOperLog.setModifier(name);
        sysOperLogMapper.updateByPrimaryKeySelective(sysOperLog);
    }

    /**
     * @Author: zql
     * @Description: 更新操作日志列表信息
     * @param sysOperLogList 更新的对象主体内容列表
     * @Data: 2021-08-06
     */
    public void updateList(List<SysOperLog> sysOperLogList) {
        String name = SecurityUtils.getUsername();
        sysOperLogList.forEach(o -> {
            o.setCreator(name);
            o.setModifier(name);
            o.setUpdateTime(null);
            o.setCreateTime(null);
        });
        sysOperLogMapper.updateListSelective(sysOperLogList);
    }

    /**
     * @Author: zql
     * @Description: 更新或创建
     * @param sysOperLogList 更新或创建的对象主体内容列表
     * @Data: 2021-08-06
     */
    public void updateAndInsert(List<SysOperLog> sysOperLogList) {
        List<SysOperLog> updateList = new ArrayList<>();
        List<SysOperLog> createList = new ArrayList<>();
        for (SysOperLog sysOperLog : sysOperLogList){
            if (StringUtils.isNotNull(sysOperLog.getSysOperLogId()) &&
                    sysOperLogMapper.has(sysOperLog.getSysOperLogId(), null) >0){
                updateList.add(sysOperLog);
            } else {
                createList.add(sysOperLog);
            }

        }
        if (updateList.size() > 0) {
            updateList(updateList);
        }
        if (createList.size() > 0) {
            createList(createList);
        }
    }

    /**
     * @Author: zql
     * @Description: 查看操作日志列表
     * @param baseEntity 基础类
     * @Data: 15:02 2021/6/3
     */
    public List<SysOperLog> getList(String title,Byte businessType,Byte status,BaseEntity baseEntity) {
        PageHelper.clearPage();
        PageHelper.startPage(baseEntity.getPage(), baseEntity.getPageSize(), baseEntity.getSortRow() + baseEntity.getSortOrder());
        return sysOperLogMapper.selectList(title,businessType,status,baseEntity.getIsDelete());
    }

    /**
     * @Author: zql
     * @Description: 查看操作日志
     * @param sysOperLogId 操作日志Id
     * @Data: 2021-08-06
     */
    public SysOperLog get(Long sysOperLogId, Byte isDelete) {
        SysOperLog sysOperLog = sysOperLogMapper.selectByPrimaryKey(sysOperLogId, isDelete);
        if (StringUtils.isNull(sysOperLog)) {
            throw new BusinessException(ErrorCodeEnum.DATA_NOT_FOUNT);
        }
        return sysOperLog;
    }

    /**
     * @Author: zql
     * @Description: 对象校验
     * @param sysOperLog 校验的对象
     * @param toUpdate 确定对象是更新还是创建
     * @Data: 2021-08-06
     */
    public void verifyInfo(SysOperLog sysOperLog, boolean toUpdate) {
        boolean toInsert = !toUpdate;
        if (StringUtils.isNotNull(sysOperLog.getIsDelete())) {
            if (!DataStatus.contains(sysOperLog.getIsDelete())) {
                log.error("数据状态设置错误:{}", sysOperLog.getIsDelete());
                throw new BusinessException(ErrorCodeEnum.USER_VERIFY.setParam("数据状态设置错误"));
            }
        }
        if (toUpdate && (StringUtils.isNull(sysOperLog.getSysOperLogId()) || sysOperLogMapper.has(sysOperLog.getSysOperLogId(), null) == 0)){
            throw new BusinessException(ErrorCodeEnum.DATA_NOT_FOUNT);
        }
    }

    public boolean has(Long sysOperLogId,Byte isDelete){
        return sysOperLogMapper.has(sysOperLogId,isDelete) >0;
    }
}
